/**
 * Created by IAN on 2016/10/14.
 */
var sel = $.Params.create();
sel.addParams("page",0);
sel.addParams("rows",rows);
sel.addParams("deleteState",1);
$(function () {

    getData();

    //查询
    $("#searchBtn").on("click",function(){
        var name = $("#name").val().trim();
        var tel = $("#tel").val().trim();

        if(name != ""){
            sel.addParams("name",name);
        }else{
            sel.addParams("name",undefined);
        }
        if(tel != ""){
            sel.addParams("tel",tel);
        }else{
            sel.addParams("tel",undefined);
        }

        getData();
    })

    //判断checkbox状态
    $("#selAll").on("click",function () {
        if(!$(this).attr("checked")){
            $("input[type=checkbox]").attr("checked","checked")
        }else{
            $("input[type=checkbox]").removeAttr("checked");
        }
    })




});


//获取数据
function getData(){
    getPageData(sel,getIP()+"memberCard/getMemberCardPage",function(data){
        if(data.success){
            var html = "";
            $.each(data.result, function(i,val) {
                if(val.hospital_name == null){
                    val.hospital_name ="";
                }
                if(val.hospital_offices == null){
                    val.hospital_offices="";
                }
                if(val.job_title == null){
                    val.job_title="";
                }
                var checkHtml="";
                if(val.checkState == null || val.checkState =="0" || val.checkState==""){
                    val.checkState="未审核";
                    checkHtml="<button type='button' class='btn btn-primary'  onclick='hasCheck(\""+val.openId+"\",\""+val.member_id+"\",\"1\",\""+val.filed10+"\")'>通过</button><button type='button' class='btn btn-primary' style='margin-left: 2px;'  onclick='unCheck(\""+val.openId+"\",\""+val.member_id+"\",\"2\")'>不通过</button>";
                }else if(val.checkState == "1"){
                    val.checkState="已通过";
                    checkHtml="<button type='button' class='btn btn-primary' style='margin-left: 2px;'  onclick='unCheck(\""+val.openId+"\",\""+val.member_id+"\",\"2\",this)'>不通过</button>";
                }else if(val.checkState == "2"){
                    val.checkState="未通过";
                    checkHtml="<button type='button' class='btn btn-primary'  onclick='hasCheck(\""+val.openId+"\",\""+val.member_id+"\",\"1\",\""+val.filed10+"\")'>通过</button><button type='button' class='btn btn-primary' style='margin-left: 2px;'  onclick='getCause(\""+val.openId+"\",\""+val.member_id+"\",\""+val.cause+"\",\"2\",this)'>原因</button>";
                }
                html+="<tr role='row'' class='even'>" +
                    "<td><input eid='"+val.id+"' onclick='getCheck(this)' type='checkbox' class='classOption'></td>" +
                    "<td class='sorting_1'>"+val.name+"</td>" +
                    "<td>"+val.tel+"</td>"+
                    "<td>"+val.hospital_name+"</td>"+
                    "<td>"+val.hospital_offices+"</td>"+
                    "<td>"+val.job_title+"</td>"+
                    "<td><img onclick='getBigImg(this)' style='max-width:130px;max-height:60px;' src='"+getTomIP()+"data/member/"+val.card+"'/></td>"+
                    "<td>"+val.checkState+"</td>"+
                    "<td>"+checkHtml+"</td>" +
                    "</tr>";
            });
            $("#listContainer").html(html);
        }
    })
}

//认证通过
function hasCheck(openId,memberId,filed9,filed10){
    var updSel = $.Params.create();
    updSel.addParams("id",memberId);
    updSel.addParams("openId",openId);
    updSel.addParams("filed9",filed9);
    updSel.addParams("filed10",filed10);
    updSel.ajax(getIP()+"memberCard/updtMemCheck",function(data){
        if(data.success){
            alert("通过成功！");
            getData();
        }else{
            alert(data.result);
        }
    })
}
//认证不通过
function unCheck(openId,memberId,filed9,obj){
    $(".reveal-modal-bg").css("visibility","visible");
    $('#selCause').show();
    $('#selCause').reveal($(obj).data());

    $("#saveBtn").on("click",function(){
        var filed7 = $("#filed7").val().trim();
        if(filed7 == ""){
            alert("原因不能为空！");
        }else{
            var updSel = $.Params.create();
            updSel.addParams("id",memberId);
            updSel.addParams("openId",openId);
            updSel.addParams("filed7",filed7);
            updSel.addParams("filed9",filed9);
            updSel.ajax(getIP()+"memberCard/updtMemCheck",function(data){
                if(data.success){
                    alert("提交成功！");
                    $("#selCause").css("display","none");
                    $(".reveal-modal-bg").css("display","none");
                    getData();
                }else{
                    alert(data.result);
                }
            })
        }
    })

}
//方法图片
function getBigImg(obj){
    $(".reveal-modal-bg").css("visibility","visible");
    $('#selOthers').show();
    $('#selOthers').reveal($(obj).data());
    var imgUrl = $(obj).attr("src");
    var imgHtml = "<img style='max-width:400px;max-height:400px;' src='"+imgUrl+"'/>";
    $("#showImg").html(imgHtml);
}

//原因
function getCause(openId,memberId,filed7,filed9,obj){
    $(".reveal-modal-bg").css("visibility","visible");
    $('#selCause').show();
    $('#selCause').reveal($(obj).data());
	$("#filed7").val(filed7);
    $("#saveBtn").on("click",function(){
        var filed7 = $("#filed7").val().trim();
        if(filed7 == ""){
            alert("原因不能为空！");
        }else{
            var updSel = $.Params.create();
            updSel.addParams("id",memberId);
            updSel.addParams("openId",openId);
            updSel.addParams("filed7",filed7);
            updSel.addParams("filed9",filed9);
            updSel.ajax(getIP()+"memberCard/updtMemCheck",function(data){
                if(data.success){
                    alert("修改原因成功！");
                    $("#selCause").css("display","none");
                    $(".reveal-modal-bg").css("display","none");
                    getData();
                }else{
                    alert(data.result);
                }
            })
        }
    })
}